package com.bocai.dahuatech.ui.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.OrderDetailBean;
import com.bocai.dahuatech.Bean.OrderListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.MyOrderAdapter;
import com.bocai.dahuatech.ui.view.MyListView;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.SharePrefencesUtil;
import com.google.gson.Gson;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 我的订单详情
 */
public class MyOrderActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.tv_line_first)
    View vLFirst;
    @Bind(R.id.tv_circle_two)
    TextView tvCTwo;
    @Bind(R.id.tv_line_two)
    View vLTwo;
    @Bind(R.id.tv_text_two)
    TextView tvTTwo;
    @Bind(R.id.tv_circle_three)
    TextView tvCThree;
    @Bind(R.id.tv_text_three)
    TextView tvTThree;
    @Bind(R.id.mlv_myorder)
    MyListView mlvMyOrder;
    @Bind(R.id.tv_dismissorder)
    TextView tvDismissOrder;
    @Bind(R.id.tv_order_detail_date)
    TextView tvDate;
    @Bind(R.id.tv_order_detail_addr)
    TextView tvAddr;
    @Bind(R.id.tv_order_detail_name)
    TextView tvName;
    @Bind(R.id.tv_order_detail_note)
    TextView tvNote;
    @Bind(R.id.tv_ordernumber)
    TextView tvOrderNumber;
    @Bind(R.id.tv_order_state)
    TextView tvState;
    @Bind(R.id.tv_num_price)
    TextView tvNumPrice;
    @Bind(R.id.ll_dismissorder)
    LinearLayout ll_dismissorder;
    ArrayList<OrderListBean.ContentEntity.ProductListEntity> list=new ArrayList<>();
    private OrderDetailBean orderDetailBean=new OrderDetailBean();
    int fid;
    String totalPrice,state;
    @Override
    protected int contentView() {
        return R.layout.activity_my_order;
    }

    @Override
    protected void initView()
    {
        ButterKnife.bind(this);
        initToolbar(R.string.meorder);
        mToolbar.inflateMenu(R.menu.menu_myorder);
        mlvMyOrder.setFocusable(false);
        fid=getIntent().getIntExtra("fid", -1);
        totalPrice=getIntent().getStringExtra("totalprice");
        state=getIntent().getStringExtra("state");
        setState();
        getOrderDetail();
        initEvent();
    }

    /**
     * 设置订单状态
     */
    private void setState()
    {
        if(getResources().getString(R.string.ordertotrue).equals(state))
        {

        }else if(getResources().getString(R.string.ordertypecomplete).equals(state))
        {
            tvCTwo.setBackgroundResource(R.drawable.text_circle_red_shape);
            tvCTwo.setTextColor(Color.parseColor("#ffffff"));
            vLFirst.setBackgroundColor(Color.parseColor("#e70014"));
            tvCThree.setBackgroundResource(R.drawable.text_circle_red_shape);
            tvCThree.setTextColor(Color.parseColor("#ffffff"));
            vLTwo.setBackgroundColor(Color.parseColor("#e70014"));
            tvTTwo.setTextColor(Color.parseColor("#e70014"));
            tvTThree.setTextColor(Color.parseColor("#e70014"));
        }else
        {
            tvCTwo.setBackgroundResource(R.drawable.text_circle_red_shape);
            tvCTwo.setTextColor(Color.parseColor("#ffffff"));
            vLFirst.setBackgroundColor(Color.parseColor("#e70014"));
            tvTTwo.setTextColor(Color.parseColor("#e70014"));
        }
    }

    private void getOrderDetail() {
        getTestApi().getOrderDetail(fid, SharePrefencesUtil.getCustomerCode(this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            if(baseBean.getReturnNo().equals("1"))
            {

                String result= AESUtils.decode(baseBean.getContent());
                result=start+result+end;
                Log.e("result",result);
                orderDetailBean=new Gson().fromJson(result,OrderDetailBean.class);
                setDatas();
            }else{
                showToast(baseBean.getReturnInfo());
            }
        }
    };

    private void setDatas() {
        tvDate.setText(getResources().getString(R.string.ordertime)+ orderDetailBean.getContent().getReceivingInfoList().get(0).getFDate());
        tvAddr.setText(getResources().getString(R.string.orderaddress)+ orderDetailBean.getContent().getReceivingInfoList().get(0).getFAddress() +
                orderDetailBean.getContent().getReceivingInfoList().get(0).getFAreaName());
        tvName.setText(getResources().getString(R.string.orderperson)+ orderDetailBean.getContent().getReceivingInfoList().get(0).getFConsignee());
        tvNote.setText(getResources().getString(R.string.ordernote) + orderDetailBean.getContent().getReceivingInfoList().get(0).getFSendNote());
        tvOrderNumber.setText(getResources().getString(R.string.ordernumbers)+orderDetailBean.getContent().getReceivingInfoList().get(0).getFBillNo());
        tvState.setText(state);
        tvNumPrice.setText(totalPrice);
        if(state.equals(getResources().getString(R.string.ordertotrue))){
            ll_dismissorder.setVisibility(View.VISIBLE);
            tvDismissOrder.setText(R.string.dismissorder);
        }else if(state.equals(getResources().getString(R.string.ordertypealready)))
        {
            ll_dismissorder.setVisibility(View.VISIBLE);
            tvDismissOrder.setText(R.string.confirmreceipt);
        }else{
            ll_dismissorder.setVisibility(View.GONE);
        }
        for(int i=0;i<orderDetailBean.getContent().getProductDetailsInfoList().size();i++){

            OrderListBean.ContentEntity.ProductListEntity bean=new OrderListBean.ContentEntity.ProductListEntity();
            bean.setFItemName(orderDetailBean.getContent().getProductDetailsInfoList().get(i).getFItemName());
            bean.setFOutModel(orderDetailBean.getContent().getProductDetailsInfoList().get(i).getFOutModel());
            bean.setFStandPrice(orderDetailBean.getContent().getProductDetailsInfoList().get(i).getFStandPrice());
            bean.setFAQty(orderDetailBean.getContent().getProductDetailsInfoList().get(i).getFAQty());
            bean.setFImgSrc(orderDetailBean.getContent().getProductDetailsInfoList().get(i).getFPicPath());
            list.add(bean);
        }

        mlvMyOrder.setAdapter(new MyOrderAdapter(this, list));
    }

    @Override
    protected void initEvent()
    {
        super.initEvent();
        tvDismissOrder.setOnClickListener(this);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                startActivity(new Intent(MyOrderActivity.this,SearchMyOrderActivity.class));
                return false;
            }
        });
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.tv_dismissorder:
            {

                View view= LayoutInflater.from(this).inflate(R.layout.dialog_dismiss_order,null);
                TextView tvgoShopcard= (TextView) view.findViewById(R.id.tv_deleteorder_goshopcard);
                TextView tvdelete= (TextView) view.findViewById(R.id.tv_deleteorder_delete);
                view.setBackgroundResource(R.drawable.dialog_shape);
                final AlertDialog dialog=new AlertDialog
                        .Builder(this)
                        .setView(view)
                        .show();
                //去购物车修改
                tvgoShopcard.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        goShoppingcard();
                        dialog.cancel();
                    }


                });
                //取消订单
                tvdelete.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v) {
                        dismissOrder();
                        dialog.cancel();
                    }
                });
            }



            break;
        }
    }

    private void goShoppingcard() {
        showLoading();
        getTestApi().orderCancel(SharePrefencesUtil.getFID(this),fid,1,SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        showToast(R.string.neterror);
//                        EventBus.getDefault().post(new MainEvent("goshopcart"));
//                        finish();
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        hideLoading();
                        showToast(baseBean.getReturnInfo());
                        EventBus.getDefault().post(new MainEvent("goshopcart"));
                        finish();
                    }
                });
    }

    private void dismissOrder() {
        showLoading();
        getTestApi().orderCancel(SharePrefencesUtil.getFID(this), fid, 2,SharePrefencesUtil.getCustomerCode(this))
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        showToast(R.string.neterror);
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        hideLoading();
                        showToast(baseBean.getReturnInfo());
                        finish();
                    }
                });
    }
}
